#include <iostream>
#include <ctime>
#include <pthread.h>
#include <unistd.h>
#include "Ringqueue.hpp"
#include "Task.hpp"

void *Consumer(void *args)
{
    Ringqueue<Task> *rq=static_cast<Ringqueue<Task> *>(args);
    while(true)
    {
        Task t;
        rq->Pop(&t);
        t();
        std::cout << "Consumer ->" << t.result() << std::endl;
    }
}
void *Productor(void *args)
{
    Ringqueue<Task> *rq=static_cast<Ringqueue<Task> *>(args);
    while(true)
    {
        sleep(1);
        int x=rand()%10+1;
        usleep(x*1000);
        int y=rand()%10+1;
        Task t(x,y);
        rq->Push(t);
        std::cout << "Productor ->" << t.debug() << std::endl;
    }
}

int main()
{
    srand(time(nullptr)^getpid());
    Ringqueue<Task> *rq=new Ringqueue<Task>(5);
    pthread_t c1,c2,p1,p2,p3;
    pthread_create(&c1,nullptr,Consumer,rq);
    pthread_create(&c2,nullptr,Consumer,rq);
    pthread_create(&p1,nullptr,Productor,rq);
    pthread_create(&p2,nullptr,Productor,rq);
    pthread_create(&p3,nullptr,Productor,rq);

    pthread_join(c1,nullptr);
    pthread_join(c2,nullptr);
    pthread_join(p1,nullptr);
    pthread_join(p2,nullptr);
    pthread_join(p3,nullptr);
}